#!/usr/bin/env python3
"""
Flask应用启动脚本
"""

import sys
import os
import webbrowser
import threading
import time

def start_flask_app():
    """启动Flask应用"""
    try:
        print("正在启动Flask应用...")
        
        # 导入并启动应用
        from app import app
        
        # 在新线程中启动浏览器
        def open_browser():
            time.sleep(2)  # 等待应用启动
            webbrowser.open('http://127.0.0.1:5000/')
            print("浏览器已打开，访问 http://127.0.0.1:5000/")
        
        # 启动浏览器线程
        browser_thread = threading.Thread(target=open_browser)
        browser_thread.daemon = True
        browser_thread.start()
        
        # 启动Flask应用
        print("Flask应用正在启动...")
        print("访问地址: http://127.0.0.1:5000/")
        print("按 Ctrl+C 停止服务器")
        
        app.run(debug=True, host='127.0.0.1', port=5000, use_reloader=False)
        
    except ImportError as e:
        print(f"导入错误: {e}")
        print("请确保已安装Flask: pip install flask")
        return 1
    except Exception as e:
        print(f"启动错误: {e}")
        return 1
    
    return 0

if __name__ == "__main__":
    sys.exit(start_flask_app())
